package com.HospitalOutpatient.controller;

import com.HospitalOutpatient.annotation.LoginToken;
import com.HospitalOutpatient.entity.Patient;
import com.HospitalOutpatient.entity.User;
import com.HospitalOutpatient.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/patient")
public class PatientController {

    @Autowired
    private PatientService patientService;

    @LoginToken
    @PostMapping("/getPatientByUserId")
    public Result getPatientByUserId(@RequestBody User user){
        user = UserContext.get();
        UserContext.remove();
        List<Patient> patientList = patientService.getPatientByUserId(user.getUserId());
        return new Result(Code.GET_OK,patientList,"获取病人信息成功");
    }
    @LoginToken
    @PostMapping("/addPatient")
    public Result addPatient(@RequestBody Patient patient){
        User user = UserContext.get();
        UserContext.remove();
        patient.setUserId(user.getUserId());
        boolean b = patientService.addPatinet(patient);
        if(b == false){
            return new Result(Code.SAVE_ERR,null,"添加病人信息失败");
        }
        return new Result(Code.SAVE_OK,null,"添加病人信息成功");
    }
    @LoginToken
    @PostMapping("/deletePatient")
    public Result deletePatient(@RequestBody Patient patient){
        boolean b = patientService.deletePatient(patient.getPatientId());
        if(b == false){
            return new Result(Code.DELETE_ERR,null,"删除病人信息失败");
        }
        return new Result(Code.DELETE_OK,null,"删除病人信息成功");
    }
}
